package com.allianz.webapps.secondController;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.allianz.webapps.common.CustomerContextHolder;
import com.allianz.webapps.https.interfaces.type001.pojo.User;
import com.allianz.webapps.service.BeforeUnderwritingService;
import com.allianz.webapps.service.MyPolicyService;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itextpdf.text.pdf.PdfStructTreeController.returnType;

/**
 * 我的保单承保前controller层
 * @author 刘建弋
 *
 * 2017年3月3日
 */
@Controller
@RequestMapping(value = "beforeUnderwriting")
public class BeforeUnderwritingController {
	
	@Autowired
	private BeforeUnderwritingService beforeUnderwriting;
	
	@Autowired
	private ObjectMapper objectMapper;

	
	
	@RequestMapping(value = "getUnderwrite")
	@ResponseBody
	public List<Map<String, Object>> getUnderwrite(HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		User user=(User) session.getAttribute("username");
		try {
			String u=user.getAgentId();
			long l = Long.parseLong(u);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("agentId", l);
			List<Map<String, Object>> result = beforeUnderwriting.getUnderwrite(map);
			return result;
		} catch (Exception e) {
			throw new Exception("查询失败,请重试");
		}
	}
	
	@RequestMapping(value = "checkUnderwrite")
	@ResponseBody
	public List<Map<String, Object>> checkUnderwrite(HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		User user = (User) session.getAttribute("username");
		try {
			return beforeUnderwriting.checkUnderwrite(Long.parseLong(user.getAgentId()));
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("查询失败,请重试");
		}
	}
	
	/**
	 * 按条件查询承保前的保单
	 * @param jsonParam
	 * @param session
	 * @return
	 * @throws Exception
	 * URL:/pad_marketing/beforeUnderwriting/getAcceptInsurance.action
	 */
	@RequestMapping(value = "getAcceptInsurance")
	@ResponseBody
	public List<Map<String, Object>> getAcceptInsurance(@RequestParam(value = "jsonParam") String jsonParam, HttpSession session) throws Exception {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_ALLIANZ);
		Map<String, Object> map = objectMapper.readValue(jsonParam, Map.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		GregorianCalendar gc = new GregorianCalendar();
		if(map.get("entryFrom").toString() != null && !"".equals(map.get("entryFrom").toString().trim())
		&& map.get("entryTo").toString() != null && !"".equals(map.get("entryTo").toString().trim())) {
			Date entryFrom = sdf.parse(map.get("entryFrom").toString());
			Date entryTo = sdf.parse(map.get("entryTo").toString());
			if(entryFrom.getTime() > entryTo.getTime()) {
				throw new Exception("受理日期的起始日期不能大于截止日期");
			} else {
				gc.setTime(entryFrom);
				gc.add(5, 90);
				entryFrom = gc.getTime();
				if(entryFrom.getTime() < entryTo.getTime()) {
					throw new Exception("受理日期查询区间不能超过90天");
				}
			}
		}
		User user = (User) session.getAttribute("username");
		map.put("agentId", Long.parseLong(user.getAgentId()));
		try {
			return beforeUnderwriting.getAcceptInsurance(map);
		} catch (Exception e) {
			throw new Exception("查询失败,请重试");
		}
	}
}
